package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.PayRecordBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00030;
import com.zfjt.service.PayRecordService;
import com.zfjt.service.UserService;

/**
 * 账号充值
 * @author xk
 * @version 1.0
 * @date 2017年4月12日
 */
@Controller
public class PayController {
	
	@Autowired
	private UserService mUser;
	
	@Autowired
	private PayRecordService mPayRecord;
	
	private ReturnBean mReturn;
	
	private Map<String, String> fhdata;
	
	@RequestMapping(value="00030")
	@ResponseBody
	public ReturnBean Login(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00030 no00030 = JSON.parseObject(data,PackNum00030.class);
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00030");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			if (no00030.getPassword().equals("8671598")) {
				List<PayRecordBean> list = mPayRecord.selectAll();
				//充值记录存到表里
				mPayRecord.insert(new PayRecordBean(list.size() + 1, no00030.getReceive_account(), no00030.getPay_account(), no00030.getDate(),
						no00030.getMoney(), no00030.getZhifubao_id(), no00030.getAuditor()));
				UserBean mm = mUser.selectByPrimaryKey(no00030.getReceive_account());
				//把账号里本来的金额拿出来和要充值的金额相加
				int money = Integer.parseInt(mm.getMoney()) + Integer.parseInt(no00030.getMoney());
				System.out.println("测试金额====" + money);
				mm.setMoney(money + "");
				mUser.updateByPrimaryKeySelective(mm);
				mReturn.setCode("000");
				mReturn.setPortnum("00030");
				fhdata.put("balance", money + "");
				mReturn.setData(JSON.toJSONString(fhdata));
			}else {
				mReturn.setCode("002");
				mReturn.setPortnum("00030");
				fhdata.put("error", "PASSWORD_ERROR");
				mReturn.setData(JSON.toJSONString(fhdata));
			}
		}
		return mReturn;
	}
}
